﻿using Infrastructure.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class EnumExtend
    {
        /// <summary>
        /// 获取枚举类型所有字段的名称
        /// </summary>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static string[] GetNames(this Enum e)
        {
            return Enum.GetNames(e.GetType());
        }


        /// <summary>
        /// 获取枚举类型所有字段的值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static T[] GetValues<T>(this Enum e)
        {
            return Enum.GetValues(e.GetType()).Cast<T>().ToArray();
        }

        /// <summary>
        /// 获取枚举类型所有字段的值
        /// </summary>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static Enum[] GetValues(this Enum e)
        {
            return e.GetValues<Enum>();
        }

        /// <summary>
        /// 获取枚举字段的Display特性的名称
        /// </summary>
        /// <param name="e">枚举字段</param>
        /// <returns></returns>
        public static string GetDisplay(this Enum e)
        {
            var field = e.GetType().GetField(e.ToString());
            var display = Attribute.GetCustomAttribute(field, typeof(EnumDisplayAttribute)) as EnumDisplayAttribute;
            return display == null ? field.Name : display.DisplayName;
        }

        /// <summary>
        /// 获取枚举类型所有字段的Display特性的名称
        /// </summary>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static string[] GetDisplays(this Enum e)
        {
            return e.GetValues().Select(item => item.GetDisplay()).ToArray();
        }

        /// <summary>
        /// 获取枚举类型所有字段名称和对应的Dispaly特性名称
        /// </summary>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<string, string>> GetNameDisplays(this Enum e)
        {
            return e.GetValues().Select(item => new KeyValuePair<string, string>(item.ToString(), item.GetDisplay()));
        }

        /// <summary>
        /// 获取枚举类型所有字段值和对应的Dispaly特性名称
        /// </summary>
        /// <param name="e">枚举类型</param>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<object, string>> GetValueDisplays(this Enum e)
        {
            return e.GetValues().Select(item => new KeyValuePair<object, string>(item, item.GetDisplay()));
        }
    }
}
